package net.qhzw.vip.qiyu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.entity.DtsCarFeedback;
import net.qhzw.vip.qiyu.entity.DtsConfig;
import net.qhzw.vip.qiyu.entity.DtsUser;
import net.qhzw.vip.qiyu.mapper.DtsCarFeedbackMapper;
import net.qhzw.vip.qiyu.model.SendSmsParam;
import net.qhzw.vip.qiyu.service.DtsCarFeedbackService;
import net.qhzw.vip.qiyu.service.DtsConfigService;
import net.qhzw.vip.qiyu.service.DtsUserService;
import net.qhzw.vip.qiyu.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DtsCarFeedbackServiceImpl extends
        ServiceImpl<DtsCarFeedbackMapper, DtsCarFeedback> implements DtsCarFeedbackService {

    @Autowired
    private DtsCarFeedbackMapper dtsCarFeedbackMapper;

    @Autowired
    private DtsConfigService dtsConfigService;

    @Autowired
    private DtsUserService dtsUserService;


    @Autowired
    private SmsService smsService;

    /***
     * 执行租车满~天后优惠活动
     */
    @Override
    public void execute() {
        List<DtsConfig> dtsConfigs = dtsConfigService.list(new LambdaQueryWrapper<DtsConfig>().eq(DtsConfig::getType, "FEEDBACK"));
        for (DtsConfig dtsConfig : dtsConfigs) {
            List<Long> ids = list(new LambdaQueryWrapper<DtsCarFeedback>().eq(DtsCarFeedback::getConfId, dtsConfig.getId())).stream().map(DtsCarFeedback::getOrderId).collect(Collectors.toList());
            List<DtsCarFeedback> list = dtsCarFeedbackMapper.findFeedback(ids, -Long.valueOf(dtsConfig.getValue()));
            list.forEach(data -> data.setConfId(dtsConfig.getId()));
            log.info("list", JSON.toJSONString(list, true));
            saveBatch(list);
        }
        send(list(new LambdaQueryWrapper<DtsCarFeedback>().eq(DtsCarFeedback::getStatus, 1)));
    }

    private void send(List<DtsCarFeedback> list) {
        if (!CollectionUtils.isEmpty(list)) {
            for (DtsCarFeedback dtsCarFeedback : list) {
                DtsUser dtsUser = dtsUserService.getById(dtsCarFeedback.getUserId());
                DtsConfig dtsConfig = dtsConfigService.getById(dtsCarFeedback.getConfId());
                if (dtsUser != null && !StringUtils.isEmpty(dtsUser.getMobile()) && dtsConfig != null && !StringUtils.isEmpty(dtsConfig.getDesc())) {
                    String mobile = dtsUser.getMobile();
                    String desc = dtsConfig.getDesc();
                    smsSend(new SendSmsParam());
                    log.info("【给用户发送优惠活动】： 用户：{}， 内容：{}", mobile, desc);
                }
            }
        }
        list.forEach(data -> data.setStatus((byte) 0));
        updateBatchById(list);
    }

    /***
     * 短信通知
     * @param sendSmsParam
     */
    private void smsSend(SendSmsParam sendSmsParam) {
        smsService.send(sendSmsParam);
    }


}
